﻿using System;
using System.Collections.Generic;
using personalplaner.business.common;
using personalplaner.business.stamm;
using personalplaner.common;
using personalplaner.dataprovider;

namespace personalplaner.servicemodel.dataaccess
{
	#region Interface

	public interface IAbteilungDataAccess
    {
		#region Get Methods

        IEnumerable<Abteilung> GetAbteilungen(int abteilungid = 0, bool addEmptyRow = false);

        IEnumerable<PersonAbteilung> GetPersonAbteilungen(int prsid, bool assigned, bool addEmptyRow = false);

        IEnumerable<PersonAbteilung> GetPersonAbteilungen(int prsid, bool assigned, DateTime date, bool addEmptyRow = false);

		IEnumerable<PersonAbteilung> GetPersonAbteilungen(int prsid);

		bool GetIsAbteilungUsed(int abteilungid);

        int GetAbteilungId(string p);

		#endregion

		#region Set Methods

        /// <summary>
        /// adds a abteilung to the person with the prsid
        /// </summary>
        /// <param name="prsid"></param>
        /// <param name="abteilungid"></param>
        /// <param name="von"></param>
        /// <param name="bis"></param>
        /// <param name="prozent"></param>
        /// <returns></returns>
        bool SetPersonAbteilung(int prsid, int abteilungid, DateTime von, DateTime bis, int prozent);

        /// <summary>
        /// adds a new abteilung
        /// </summary>
        /// <param name="abteilung"></param>
        /// <returns></returns>
		bool SetAbteilung(Abteilung abteilung);

		#endregion

		#region Update Methods

		bool UpdateAbteilung(Abteilung abteilung);

		#endregion

		#region Delete Methods

		bool RemovePersonAbteilung(int prsid, int abteilungid, DateTime datevon, DateTime datebis);

        bool DeletePersonAbteilung(int prsid, int abteilungid, DateTime datevon, DateTime datebis);

		bool DeleteAbteilung(int abteilungid);

		#endregion
    }

	#endregion

	public class AbteilungDataAccess : BaseDataAccess, IAbteilungDataAccess
    {
        public AbteilungDataAccess(ISettingsProvider settings)
            : base(settings)
        {
        }

		#region Get Methods

		public IEnumerable<Abteilung> GetAbteilungen(int abteilungid = 0, bool addEmptyRow = false)
        {
			using (new WaitCursor())
			{
				try
				{
					return DataServices.AbteilungService.GetAbteilungen(Settings.DatabaseSettings, Settings.Mandant, abteilungid: abteilungid, addEmptyRow: addEmptyRow);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
                    Console.Write(e);
				}

				return null;
			}
        }

        public IEnumerable<PersonAbteilung> GetPersonAbteilungen(int prsid, bool assigned, bool addEmptyRow = false)
        {
            return GetPersonAbteilungen(prsid, assigned, DateTime.Today, addEmptyRow);
        }

        public IEnumerable<PersonAbteilung> GetPersonAbteilungen(int prsid, bool assigned, DateTime date, bool addEmptyRow = false)
        {
			using (new WaitCursor())
			{
				try
				{
                    return DataServices.AbteilungService.GetPersonAbteilungen(Settings.DatabaseSettings, Settings.Mandant, prsid, assigned, date, addEmptyRow);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
                    Console.Write(e);
				}

				return null;
			}
        }

		/// <summary>
		/// ehemals DataView GetPersonAbteilungHistory(int prsid)
		/// [PrsId,AbteilungID,Name,Von,Bis,Prozent,Status,Statustext]
		/// </summary>
		/// <param name="prsid"></param>
		/// <returns></returns>
		public IEnumerable<PersonAbteilung> GetPersonAbteilungen(int prsid/*, DateTime date = DateTime.Today*/)
		{
			using (new WaitCursor())
			{
				try
				{
                    return DataServices.AbteilungService.GetPersonAbteilungen(Settings.DatabaseSettings, prsid, DateTime.Today);
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}

				return null;
			}
        }

		public bool GetIsAbteilungUsed(int abteilungid)
		{
			using (new WaitCursor())
			{
				try
				{
                    return DataServices.AbteilungService.GetIsAbteilungUsed(Settings.DatabaseSettings, abteilungid);
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}

				return false;
			}
		}

        public int GetAbteilungId(string abteilung)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.AbteilungService.GetAbteilungId(Settings.DatabaseSettings, abteilung);
                }
                catch (Exception)
                {
                    //TODO: show a messagebox here with the error
                }

                return 0;
            }
        }

		#endregion

		#region Set Methods

        /// <summary>
        /// adds a abteilung to the person with the prsid
        /// </summary>
        /// <param name="prsid"></param>
        /// <param name="abteilungid"></param>
        /// <param name="von"></param>
        /// <param name="bis"></param>
        /// <param name="prozent"></param>
        /// <returns></returns>
		public bool SetPersonAbteilung(int prsid, int abteilungid, DateTime von, DateTime bis, int prozent)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.AbteilungService.SetPersonAbteilung(Settings.DatabaseSettings, prsid, abteilungid, von, bis, prozent);
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}

				return false;
			}
		}

        /// <summary>
        /// adds a new abteilung
        /// </summary>
        /// <param name="abteilung"></param>
        /// <returns></returns>
		public bool SetAbteilung(Abteilung abteilung)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.AbteilungService.SetAbteilung(Settings.DatabaseSettings, abteilung, Settings.Mandant);
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}

				return false;
			}
		}

		#endregion

		#region Update Methods

		public bool UpdateAbteilung(Abteilung abteilung)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.AbteilungService.UpdateAbteilung(Settings.DatabaseSettings, abteilung);
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}

				return false;
			}
		}

		#endregion

		#region Delete Methods

		public bool RemovePersonAbteilung(int prsid, int abteilungid, DateTime datevon, DateTime datebis)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.AbteilungService.RemovePersonAbteilung(Settings.DatabaseSettings, prsid, abteilungid, datevon, datebis);
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}

				return false;
			}
		}

        public bool DeletePersonAbteilung(int prsid, int abteilungid, DateTime datevon, DateTime datebis)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.AbteilungService.DeletePersonAbteilung(Settings.DatabaseSettings, prsid, abteilungid, datevon, datebis);
                }
                catch (Exception)
                {
                    //TODO: show a messagebox here with the error
                }

                return false;
            }
        }

		public bool DeleteAbteilung(int abteilungid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.AbteilungService.DeleteAbteilung(Settings.DatabaseSettings, abteilungid);
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}

				return false;
			}
		}
		#endregion

	}
}
